package com.apes.fico.finance.model.gl;

import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.fico.masterdata.gl.model.AccountingOperatingChart;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: apestech
 * @description: 年初(辅助核算)
 * @author: Chengxm
 * @create: 2019-01-15 13:49
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InitAux.CONST_TABLE_NAME, indexes = {
        @Index(columnList = "year,period,company_id,acc_operating_chart_id,key", unique = true),
        @Index(columnList = "key")
})
public class InitAux implements ApesBean {
    public final static String CONST_TABLE_NAME = "gl_init_aux";
    @Id
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "辅助核算年初余额表", padding = 8)
    @Comment("编号")
    private String id;

    @NotNull
    @Column
    @Comment("会计年度")
    private int year;

    @NotNull
    @Column
    @Comment("会计期间")
    private int period;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;

    @NotNull
    @ManyToOne()
    @JoinColumn(name = "acc_operating_chart_id", referencedColumnName = "id")
    @Comment("科目")
    private AccountingOperatingChart accountingOperatingChart;

    @OneToMany(mappedBy = "initAux", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<InitAuxItem> initAuxItemList = new ArrayList();

    public List<InitAuxItem> getInitAuxItemList() {
        for (InitAuxItem item : initAuxItemList) {
            item.setInitAux(this);
        }
        return initAuxItemList;
    }

    @Column(name = "key", nullable = false, length = 120)
    @Comment("辅助核算") /*辅助核算类型代码+值组合*/
    private String key;

    @Column(name = "name", nullable = false, length = 400)
    @Comment("辅助内容组合")
    private String name;

    @Column(name = "un_debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("未记账借方金额")
    private double unDebit = 0;

    @Column(name = "un_credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("未记账贷方金额")
    private double unCredit = 0;


    @Column(name = "debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("借方金额")
    private double debit = 0;

    @Column(name = "credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("贷方金额")
    private double credit = 0;

    @Column(name = "opening_balance", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("期初余额") /*借方-贷方*/
    private double openingBalance = 0;
}
